package com.company.bookstore.controller.public_module;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.bookstore.common.result.MvcResult;
import com.company.bookstore.entities.book_module.Category;
import com.company.bookstore.service.book_module.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 姬深威
 * @since 2020-09-26
 */
@RestController
@Slf4j
@RequestMapping("/public/category")
public class PublicCategoryController {

    @Resource
    CategoryService categoryService;

    @GetMapping("/getList/{current}/{size}/{orderType}")
    public MvcResult<Object> getAllCategories(@PathVariable Integer current, @PathVariable Integer size, @PathVariable Integer orderType) {
            IPage<Category> categories = categoryService.getAllCategories(new Page<>(current, size),orderType);
        if (categories.getRecords().size()==0) {
            return MvcResult.SELECT_FAIL;
        } else {
            MvcResult.SELECT_SUCCESS.setObject(categories);
            return MvcResult.SELECT_SUCCESS;
        }
    }

    @GetMapping("/getCategoryById/{id}")
    public MvcResult<Object> getCategoryById(@PathVariable Integer id) {
        Category category = categoryService.getCategoryById(id);
        if (category == null) {
            return MvcResult.SELECT_FAIL;
        } else {
            MvcResult.SELECT_SUCCESS.setObject(category);
            return MvcResult.SELECT_SUCCESS;
        }
    }
}
